/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Frontera;

import BarCode.GenerateBarcode;
import BarCode.Imprimible;
import Entidad.Analisis;
import Entidad.Cliente;
import Entidad.Muestra;
import control.ControlCliente;
import control.ControlMuestra;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Juan
 */
public class FConsultaMuestra extends javax.swing.JFrame {

    /**
     * Creates new form FConsultaMuestra
     */
    public FConsultaMuestra() {
        initComponents();
        reSizeTableAnalisis();
        this.setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        TFBusqueda = new javax.swing.JTextField();
        BBuscar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        TFIdentificacion = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        TFSolicitud = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        TFNombreCliente = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        TFCantidad = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        CBRadioactiva = new javax.swing.JCheckBox();
        CBMagnetica = new javax.swing.JCheckBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        TAnalisis = new javax.swing.JTable(){
            @Override
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false;
            }
        };
        jScrollPane3 = new javax.swing.JScrollPane();
        TAObservaciones = new javax.swing.JTextArea();
        BMasCriterios = new javax.swing.JButton();
        BAceptar = new javax.swing.JButton();
        BImprimir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Consulta de Muestra");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Consultar Muestra"));

        TFBusqueda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TFBusquedaActionPerformed(evt);
            }
        });

        BBuscar.setText("Buscar");
        BBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BBuscarActionPerformed(evt);
            }
        });

        jLabel1.setText("Identificación");

        TFIdentificacion.setEditable(false);
        TFIdentificacion.setText(" ");

        jLabel2.setText("Número de Solicitud:");

        TFSolicitud.setEditable(false);
        TFSolicitud.setText(" ");

        jLabel3.setText("Nombre del Cliente");

        TFNombreCliente.setEditable(false);
        TFNombreCliente.setText(" ");

        jLabel6.setText("Cantidad (gr):");

        TFCantidad.setEditable(false);
        TFCantidad.setText(" ");

        jLabel7.setText("Observaciones:");

        CBRadioactiva.setText("Radioactiva");
        CBRadioactiva.setEnabled(false);

        CBMagnetica.setText("Magnética");
        CBMagnetica.setEnabled(false);

        TAnalisis.setModel(new ModeloTablaAnalisis());
        TAnalisis.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        TAnalisis.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(TAnalisis);

        TAObservaciones.setColumns(20);
        TAObservaciones.setEditable(false);
        TAObservaciones.setRows(5);
        jScrollPane3.setViewportView(TAObservaciones);

        BMasCriterios.setText("Más Criterios");
        BMasCriterios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BMasCriteriosActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(TFSolicitud, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                                    .addComponent(TFIdentificacion)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(TFCantidad, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                                    .addComponent(TFNombreCliente)))
                            .addComponent(jLabel6)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(TFBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(BBuscar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(BMasCriterios)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(29, 29, 29)
                                .addComponent(jLabel7))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(CBRadioactiva)
                                .addGap(18, 18, 18)
                                .addComponent(CBMagnetica))
                            .addComponent(jScrollPane3)))
                    .addComponent(jScrollPane2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(TFBusqueda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(BBuscar))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(TFIdentificacion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(TFSolicitud, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(TFNombreCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(BMasCriterios))
                        .addGap(11, 11, 11)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
                .addGap(13, 13, 13)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(TFCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(CBRadioactiva)
                        .addComponent(CBMagnetica)))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        BAceptar.setText("Aceptar");
        BAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BAceptarActionPerformed(evt);
            }
        });

        BImprimir.setText("Imprimir código de barras");
        BImprimir.setEnabled(false);
        BImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BImprimirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(BAceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(BImprimir)
                        .addGap(35, 35, 35))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(BAceptar)
                    .addComponent(BImprimir))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void TFBusquedaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TFBusquedaActionPerformed
        if(TFBusqueda.getText().isEmpty())
            JOptionPane.showMessageDialog(null, "El campo de búsqueda está vacío");
        else
            cargarMuestra();
    }//GEN-LAST:event_TFBusquedaActionPerformed

    private void BBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BBuscarActionPerformed
        if(TFBusqueda.getText().isEmpty())
            JOptionPane.showMessageDialog(null, "El campo de búsqueda está vacío");
        else
            cargarMuestra();
    }//GEN-LAST:event_BBuscarActionPerformed

    private void BImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BImprimirActionPerformed
        GenerateBarcode codigo= new GenerateBarcode();
        try {
            codigo.generarCodigo(TFBusqueda.getText());
        } catch (Exception ex) {
           
        }
        new Imprimible().imprimir(System.getProperty("user.dir") + System.getProperty("file.separator")+"src"+System.getProperty("file.separator")+"CodigosGenerados"+System.getProperty("file.separator")+TFBusqueda.getText()+".png");

    }//GEN-LAST:event_BImprimirActionPerformed

    private void BAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BAceptarActionPerformed
        dispose();
    }//GEN-LAST:event_BAceptarActionPerformed

    private void BMasCriteriosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BMasCriteriosActionPerformed
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new FBuscarMuestras().setVisible(true);
            }
        });
        dispose();
    }//GEN-LAST:event_BMasCriteriosActionPerformed
    
    private void reSizeTableAnalisis(){
        TAnalisis.getColumn("Normal").setPreferredWidth(200);
        TAnalisis.getColumn("Rush").setPreferredWidth(200);
        TAnalisis.getColumn("Análisis").setPreferredWidth(900);
        TAnalisis.setPreferredSize(new java.awt.Dimension(600, 400));
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FConsultaMuestra.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FConsultaMuestra.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FConsultaMuestra.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FConsultaMuestra.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new FConsultaMuestra().setVisible(true);
            }
        });
    }
    
    private void limpiarCampos () {
        TFBusqueda.setText(null);
        TFIdentificacion.setText(null);
        TFCantidad.setText(null);
        TFNombreCliente.setText(null);
        TFSolicitud.setText(null);
        TAObservaciones.setText(null);
        TAnalisis.setModel(new DefaultTableModel());
        BImprimir.setEnabled(false);
    }
    
    private void cargarMuestra(){
         BImprimir.setEnabled(false);
        ControlMuestra controlMuestra = new ControlMuestra();
        
        try {
            Muestra muestra = controlMuestra.buscarMuestra(TFBusqueda.getText().trim());
            Cliente cliente = new ControlCliente().cargarCliente(muestra.getAbreviaturaCliente());
            Analisis analisis = controlMuestra.cargarAnalisis(muestra.getIdMuestra());
            
            TFCantidad.setText("" + muestra.getCantidad());
            TFIdentificacion.setText(String.valueOf(muestra.getCodigoAlpha()));
            TFSolicitud.setText("" + muestra.getConsecutivoSolicitud());
            TAObservaciones.setText(muestra.getObservaciones());
            TFNombreCliente.setText(cliente.getNombre());
            CBMagnetica.setSelected(muestra.isMagnetica());      
            CBRadioactiva.setSelected(muestra.isRadioactiva());
            ModeloTablaAnalisis modeloTablaAnalisis = new ModeloTablaAnalisis();
            modeloTablaAnalisis.setValores(analisis);
            TAnalisis.setModel(modeloTablaAnalisis);
            BImprimir.setEnabled(true);
            reSizeTableAnalisis();
            
        } catch (Exception e) {
            limpiarCampos();
            JOptionPane.showMessageDialog(this, e.getMessage(), "Error de búsqueda", JOptionPane.ERROR_MESSAGE);
        }
    
}
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BAceptar;
    private javax.swing.JButton BBuscar;
    private javax.swing.JButton BImprimir;
    private javax.swing.JButton BMasCriterios;
    private javax.swing.JCheckBox CBMagnetica;
    private javax.swing.JCheckBox CBRadioactiva;
    private javax.swing.JTextArea TAObservaciones;
    private javax.swing.JTable TAnalisis;
    private javax.swing.JTextField TFBusqueda;
    private javax.swing.JTextField TFCantidad;
    private javax.swing.JTextField TFIdentificacion;
    private javax.swing.JTextField TFNombreCliente;
    private javax.swing.JTextField TFSolicitud;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    // End of variables declaration//GEN-END:variables
    
}
